import os
from pathlib import Path
import pandas as pd

path = r"F:\资料\期货\data"


def pd_to_file(df, name, sdate):
    directory = Path(f'{path}\\{name}')
    # 检查目录是否存在
    if not directory.exists():
        # 如果目录不存在，创建目录
        directory.mkdir(parents=True, exist_ok=True)
    # 将 DataFrame 写入 CSV 文件
    df.to_csv(f'{directory}\\{sdate}_{name}.csv', index=True)  # index=False 不写入行索引


def file_to_pd(name, sdate):
    directory = Path(f'{path}\\{name}')
    # 从 CSV 文件读取 DataFrame
    df_from_csv = pd.read_csv(f'{directory}\\{sdate}_{name}.csv', index_col=0)
    return df_from_csv


def file_exists(name, sdate):
    directory = Path(f'{path}\\{name}')
    pd_file_name = f'{directory}\\{sdate}_{name}.csv'
    return os.path.exists(pd_file_name)


if __name__ == '__main__':
    # 示例数据
    # data = {'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35]}
    # df = pd.DataFrame(data)
    # pd_to_file(df,"test","20241028")
    df = file_to_pd("pta#短纤", "2024-10-28")
    print(df)
